package com.itao.springboot.controller;

import com.itao.springboot.bean.User;
import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Map;

/**
 * rust reqwest测试
 * @author sjt
 * @Date 2024/11/19 14:56
 */
@RestController
public class ReqwestController {

    @GetMapping("/get")
    public User get(@RequestParam String name){
        System.out.println("get param: " + name);
        return new User("sjt");
    }

    @PostMapping("/post")
    public String post(@RequestBody User user){
        System.out.println("post param: " + user);
        return "post method";
    }


    @PostMapping("/file")
    public String file(MultipartFile file, User user) throws IOException {
        System.out.println(user);
        int available = file.getInputStream().available();
        System.out.println("文件名：" + file.getName() + ", 大小：" + available);
        return "file upload";
    }

    @PostMapping("/cookie1")
    public String cookie1(HttpServletResponse response){
        Cookie cookie = new Cookie("username", "sjt");
        //将cookie对象加入response响应
        response.addCookie(cookie);
        return "cookie1";
    }

    @PostMapping("/cookie2")
    public String cookie2(@CookieValue("username") String username) {
        System.out.println("cookie: " + username);
        return "cookie2";
    }

    @PostMapping("/test")
    public String test(@RequestBody User user) {
        System.out.println("cookie: " + user.getName());
        return "cookie2";
    }

    @PostMapping("/data")
    public User data(User user, HttpServletRequest request,@RequestHeader Map<String, String> headers) {
        String name = request.getParameter("name");
        boolean isMale = Boolean.parseBoolean(request.getParameter("isMale"));
        System.out.println(name);
        System.out.println(isMale);
        System.out.println(user);
        System.out.println(headers);
        return user;
    }

    @PostMapping("/json")
    public User json(@RequestBody User user) {
        System.out.println(user);
        return user;
    }

    @PostMapping("/form")
    public User form(User user) {

        return user;
    }
}



